"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
  class Course extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      models.Course.belongsTo(models.Category, { foreignKey: "CategoryId", as: "category" });
      models.Course.belongsTo(models.User, { foreignKey: "UserId", as: "user" });
      models.Course.hasMany(models.Chapter, { as: "chapters" });
      models.Course.belongsToMany(models.User, {through: 'Like'})
    }
  }
  Course.init(
    {
      CategoryId: {
        type: DataTypes.INTEGER,
        allowNull:false,
        validate: {
          notNull: '所属分类不能为null',
          notEmpty: '所属分类不能为空',
        }
      },
      UserId: {
        type: DataTypes.INTEGER,
        allowNull:false,
        validate: {
          notNull: '所属用户不能为null',
          notEmpty: '所属用户不能为空',
        }
      },
      name: {
        type: DataTypes.STRING,
        allowNull:false,
        validate: {
          notNull: '课程名不能为null',
          notEmpty: '课程名不能为空',
        }
      },
      likeCount: DataTypes.INTEGER,
      isRecommend: DataTypes.BOOLEAN,
      imgUrl: DataTypes.STRING,
      introduce: DataTypes.TEXT,
      content: DataTypes.TEXT,
      rank: DataTypes.INTEGER,
      chapterCount: DataTypes.INTEGER,
      introductory: {
        type: DataTypes.BOOLEAN,
        defaultValue: false
      }
    },
    {
      sequelize,
      modelName: "Course",
    }
  );
  return Course;
};
